﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using SFast.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Objects;
using System.Linq.Expressions;
using NLite.Data;
//using EntityFramework.Extensions;


namespace SolutionDAL
{
    public class T_UsersInRolesDAL
    {

        #region 通过用户ID获取该用户的所有角色信息
        /// <summary>
        /// 通过用户ID获取该用户的所有角色信息
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns></returns>
        public List<T_AdminRole> GetRolesByUser(String userid)
        {
            PurviewDBEntities dbe = DBConfig.GetDBContext();
            //var q = dbe.T_UsersInRoles
            //.Where(o => o.UserID == userid)
            //.Join(dbe.T_AdminRoles, r1 => r1.RoleID, r2 => r2.RoleID, (r1, r2) => r2);
            var q = dbe.T_UsersInRoles.Where(o => o.UserID == userid).Select(o=> o.Role);
            return q.ToList();
        }
        #endregion

        #region 通角色ID获取该角色下的所有用户
        /// <summary>
        /// 通角色ID获取该角色下的所有用户
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <param name="userNameKey">用户姓名、用户名关键字</param>
        /// <returns></returns>
        public IQueryable<T_AdminUser> getUserByRoleID(String roleid, String userNameKey)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            var q = db.T_UsersInRoles
                .Where(o => o.RoleID == roleid);
            if (!String.IsNullOrEmpty(userNameKey))
            {
                q = q.Where(o => o.User.UserName.Contains(userNameKey) || o.User.Account.Contains(userNameKey));
            }
            var u = q.Select(o => o.User);
            return u;
        }
        #endregion

        #region 添加或更新管理员的所属角色(webform)
        /// <summary>
        /// 添加或更新管理员的所属角色
        /// 标记：需要修改
        /// </summary>
        /// <param name="userid">当前管理员ID</param>
        /// <param name="list">所属角色的角色ID集合</param>
        public void AddOrUpdate(String userid, List<String> list)
        {

            PurviewDBEntities dbe = DBConfig.GetDBContext();
            dbe.UsingTransaction(() =>
            {
                try
                {
                    //删除当前用户的所有角色
                    dbe.T_UsersInRoles.Delete(o => o.UserID == userid);
                    T_UsersInRole tuir = null;
                    foreach (var item in list)
                    {
                        tuir = new T_UsersInRole();
                        tuir.RoleID = item;
                        tuir.UserID = userid;
                        dbe.T_UsersInRoles.Insert(tuir);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            });

        }
        #endregion

        #region 添加用户，给制定的角色添加用户
        /// <summary>
        /// 添加用户，给制定的角色添加用户
        /// </summary>
        /// <param name="uir">T_UsersInRole 对像</param>
        /// <returns></returns>
        public int addUserForRole(T_UsersInRole uir)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            int r = db.T_UsersInRoles.Insert(uir);
            return r;
        }

        /// <summary>
        /// 添加用户，给制定的角色添加用户
        /// </summary>
        /// <param name="uir">T_UsersInRole 集合</param>
        /// <returns></returns>
        public int addUserForRole(List<T_UsersInRole> uir)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            int r = 0;
            db.UsingTransaction(() => {
                foreach (T_UsersInRole item in uir)
                {
                    r= addUserForRole(item);
                }
            });
            return r;
        }
        #endregion

        #region 删除指定角色下的用户关系
        /// <summary>
        /// 删除关系，通过关系ID
        /// </summary>
        /// <param name="list">id 集</param>
        public int deleteUserByRoleID(String roleid, List<String> list)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            int r = 0;
            db.UsingTransaction(() =>
            {
                r= db.T_UsersInRoles.Delete(o => o.RoleID == roleid && list.Contains(o.UserID));
            });
            return r;
        }
        #endregion

        #region  批量删除、批量更新
        //public String DeleteRolesByUser(string userid)
        //{
        //    //var q = etdb.T_AdminRoles.Update(
        //    //        o => o.RoleID == "1", 
        //    //        o2 => new T_AdminRoles { RoleName="这个要更新" }
        //    //        );


        //    List<String> l = new List<string>() { "ID1", "ID2", "ID3", "ID4" };
        //    var t = etdb.T_AdminRoles.Delete(o => l.Contains(o.RoleName));

        //    return "";
        //    //return "";
        //}
        #endregion

    }
}
